﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using TarreOA.TarreOAModels;

namespace TarreOA.TarreOADAL {
    public class Flow_WorkFeedBack_DAO {
        /// <summary>
        /// 根据指定条件更改数据库中某些记录
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="sqlParas"></param>
        /// <returns></returns>
        public int UpdateByTest(string sql, Hashtable sqlParas) {
            int count = 0;
            SqlConnection sqlConn = null;
            SqlCommand sqlCmd = null;
            try {
                sqlConn = SQLManager.GetConn();
                sqlConn.Open();
                sqlCmd = new SqlCommand(sql, sqlConn);
                foreach(object key in sqlParas.Keys) {
                    sqlCmd.Parameters.Add(new SqlParameter(key.ToString(), sqlParas[key]));
                }
                count = sqlCmd.ExecuteNonQuery();
            } catch(Exception ex) {
                Console.Write(ex.Message);
            } finally {
                if(sqlConn != null && sqlConn.State == ConnectionState.Open) {
                    sqlConn.Close();
                    sqlConn.Dispose();
                }
            }
            return count;
        }

        /// <summary>
        /// 根据指定条件返回查询结果集
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="sqlParas"></param>
        /// <param name="sqlConn"></param>
        /// <returns></returns>
        public SqlDataReader FindByTest(string sql, Hashtable sqlParas, SqlConnection sqlConn) {
            SqlDataReader sqlDr = null;
            SqlCommand sqlCmd = null;
            try {
                if(sqlConn != null && sqlConn.State != ConnectionState.Open) {
                    sqlConn.Open();
                }
                sqlCmd = new SqlCommand(sql, sqlConn);
                foreach(object key in sqlParas.Keys) {
                    sqlCmd.Parameters.Add(new SqlParameter(key.ToString(), sqlParas[key]));
                }
                sqlDr = sqlCmd.ExecuteReader();
            } catch(Exception ex) {
                Console.WriteLine(ex.Message);
            }
            return sqlDr;
        }
    }
}
